// pages/personal/iphone/iphone.js
let http = require('../../../utils/api.js');
import {
    myHttps,
    login
} from '../../../utils/util.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sign: '', //手机验证码加密参数
        blue: true, //显示蓝色 获取验证码
        time: 59, //倒计时数字
        telphone: '', //电话号码
        codes: '', //获取验证码
        userId:'',//用户id
        iphone:false,//控制绑定页面的显示隐藏
        goBindIphone:0,//控制绑定显示隐藏
        mobile:'',//已经绑定的电话
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            userId: options.userId
        })
        this.GetInfo();//用户详情
    },
    // 获取用户信息接口 
    GetInfo() {
        var url = http.api.userInfo + this.data.userId;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                if (res.data.mobile) {
                    this.setData({
                        mobile: res.data.mobile,
                        goBindIphone: 1
                    })
                }else {
                    this.setData({
                        goBindIphone: 0
                    })
                }
            }
        }).catch(err => { })
    },
    // 点击绑定手机号 或者解绑手机号
    goBindIphone () {
        this.setData({
            iphone:true,
            goBindIphone:3
        })
    },
    // 获取手机号码
    getPhone(e) {
        var val = e.detail.value;
        //赋值给telphone
        this.setData({
            telphone: val
        });
        
    },
    // 点击获取验证码
    btn() {
        var that = this
        // 正则验证手机号码格式
        var phone = that.data.telphone
        // console.log(phone)
        if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
            wx.showToast({
                title: '手机号码有误，请重新填写',
                icon: 'none',
                mask: false,
                duration: 800,
            })
            return false;
        }
        // 验证手机号码是否可用
        var url = http.api.isiphone;
        var pramas = {
            mobile: this.data.telphone,
        };
        myHttps({
            url: url,
            method: "POST",
            data: pramas
        }).then(res => {
            //手机号码验证成功后 走获取验证码的接口
            if (res.code == 1) {
                setTimeout(function () {
                    that.yanzheng()
                }, 1000)
            } else if (res.code == 0) {
                wx.showToast({
                    title: '该手机号码已经绑定!',
                    icon: 'none',
                    duration: 1000,
                    mask: true,
                })
            }
        }).catch(err => { })
    },
    // 获取验证码的接口
    yanzheng() {
        var that = this
        var url = http.api.sendCode;
        var pramas = {
            mobile: that.data.telphone,
        };
        myHttps({
            url: url,
            method: "POST",
            data: pramas
        }).then(res => {
            that.setData({
                blue: false,
                time: 59
            });
            this.getCode()
        }).catch(err => { })
    },
    getCode: function () {
        var that = this;
        var currentTime = that.data.time;
        that.setData({
            time: 59
        })
        var interval = setInterval(function () {
            that.setData({
                time: (currentTime - 1) 
            })
            currentTime--;
            if (currentTime <= 0) {
                clearInterval(interval)
                that.setData({
                    
                    blue: true
                })
            }
        }, 1000)
    },
    // 获取验证码输入框里面的值
    codes(e) {
        var val = e.detail.value;
        this.setData({
            codes: val
        });
    },
   
    // 点击确定 提交数据 走接口
    submit() {
        if (this.data.telphone == '') {
            wx.showToast({
                icon:'none',
                title: '手机号不能为空',
            })
        } else if (this.data.codes == '') {
            wx.showToast({
                icon: 'none',
                title: '验证码不能为空',
            })
        }else {
            let url = http.api.yzCode + this.data.userId + '/mobile/bind';
            let pramas = {
                code: this.data.codes,
                mobile: this.data.telphone,
            };
            myHttps({
                url: url,
                method: "POST",
                data: pramas
            }).then(res => {
                if (res.code == 1) {
                    wx.showToast({
                        title: '手机号绑定成功',
                        icon: 'success',
                        duration: 1000
                    })
                    setTimeout(function () {
                        wx.switchTab({
                            url: '../home/home',
                        })
                    }, 1000)
                }
            }).catch(err => { })
        }   
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})